﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Parkour : MonoBehaviour {

    public static Parkour Instance;

    public Animator ani;
    public GameObject XiaoQi;
    private float speed = 7;
    public bool canRun = false;
    public bool canTouch = false;

    private Vector2 touchFirst = Vector2.zero;
    private Vector2 touchSecond = Vector2.zero;
    private AudioSource audios;

    public int i = 1;
    public AudioClip RunClip;
	
	void Start ()
    {
        Instance = this;
        audios = Camera.main.GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (canRun==true)
        {
            //执行一次
            if (i == 1)
            {
                ani.SetTrigger("Run");

                audios.clip = RunClip;
                audios.Play();
                audios.loop = true;

                Camera.main.GetComponent<CameraFollow>().enabled = true;
                i += 1;
            }
            XiaoQi.transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.World);
        }
        //点击开始跑动
        if (canTouch==true && Input.GetMouseButton(0))
        {
            canRun = true;
            canTouch = false;
        }
    }

    public void Death()
    {
        canRun = false;
        ani.SetTrigger("Death");
        StartCoroutine("DeathPanel");
    }

    IEnumerator DeathPanel()
    {
        yield return new WaitForSeconds(1);
        Instantiate(Resources.Load("Prefabs/UI/GameOver"), GameObject.Find("Canvas").transform);
    }

    private void OnGUI()
    {
        if (canRun==true)
        {
            if (Event.current.type == EventType.mouseDown)
            {
                touchFirst = Event.current.mousePosition;
            }
            if (Event.current.type == EventType.mouseDrag)
            {
                touchSecond = Event.current.mousePosition;
                if (touchSecond.x < touchFirst.x)
                {
                    Debug.Log("左划");
                    XiaoQi.transform.DORotate(new Vector3(0, -30, 0), 0.1f).OnComplete(() =>
                    {
                        XiaoQi.transform.DORotate(new Vector3(0, 0, 0), 0.1f);
                    });
                    if (XiaoQi.transform.localPosition.x == 0)
                    {
                        XiaoQi.transform.DOLocalMoveX(-2.5f, 0.5f);
                    }
                    else if (XiaoQi.transform.localPosition.x == 2.5f)
                    {

                        XiaoQi.transform.DOLocalMoveX(0, 0.5f);
                    }

                }
                else if (touchSecond.x > touchFirst.x)
                {
                    Debug.Log("右划");
                    XiaoQi.transform.DORotate(new Vector3(0, 30, 0), 0.1f).OnComplete(() =>
                    {
                        XiaoQi.transform.DORotate(new Vector3(0, 0, 0), 0.1f);
                    });
                    if (XiaoQi.transform.localPosition.x == 0)
                    {
                        XiaoQi.transform.DOLocalMoveX(2.5f, 0.5f);
                    }
                    else if (XiaoQi.transform.localPosition.x == -2.5)
                    {
                        XiaoQi.transform.DOLocalMoveX(0, 0.5f);
                    }
                }
                touchFirst = touchSecond;
            }
        }
        
    }

    
}
